/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bgate.screen;

import com.bgate.core.Debug;
import com.bgate.core.Screen;
import com.bgate.game.MainGame;
import com.bgate.object.Asset;
import com.bgate.object.Character;
import com.bgate.object.LiuKang;
import com.bgate.object.LoadCharacter;
import com.bgate.object.Skill;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author thinhnv
 */
public class GameScreen extends Screen {

    Character character;
    Character characterAuto;
    LayerManager lm = new LayerManager();
    public Sprite map;
    public int previousKey = 0;
    public int count = 0;
    public Vector listSkill = new Vector();
    public boolean controlIsLeft = true;
    public int variableBackUpAuto;
    public int variableBackUp;
    public boolean checkCollisionSpace = true;
    public int viewCenterX;
    public int viewCenterY;
    public boolean endGame = false;

    public GameScreen(MainGame game) {
        super(game);
        new LoadCharacter();
        // Choise character in here
        map = new Sprite(Asset.map1);
        characterAuto = new LiuKang(2, this);
        character = new LiuKang(this, characterAuto);
        characterAuto.setRival(character);
        //===================
        map = new Sprite(Asset.map1);
        map.setPosition(0, this.getHeight() - this.map.getHeight());
        lm.setViewWindow((map.getWidth() - this.getWidth()) / 2, (getHeight() > map.getHeight()) ? (0) : (map.getHeight() - getHeight()), this.getWidth(), this.getHeight());
        reAdd();
    }

    public void reAdd() {
        for (int i = 0; i < lm.getSize(); i++) {
            lm.remove(lm.getLayerAt(i));
        }
        for (int i = 0; i < listSkill.size(); i++) {
            lm.append((Skill) listSkill.elementAt(i));
        }
        lm.append(character);
        lm.append(characterAuto);
        if(endGame){
            lm.append(Asset.drark);
        }
        lm.append(map);
    }

    public void init() {
    }

    public void update() {
        updateKey();
        reAdd();
        Debug.d("" + this.character.getX() + "  " + this.characterAuto.getX());
        character.update();
        characterAuto.update();
        if (character.getX() < characterAuto.getX()) {
            if (!controlIsLeft) {
                this.change();
            }
        } else {
            if (controlIsLeft) {
                this.change();
            }
        }
        for (int i = 0; i < listSkill.size(); i++) {
            ((Skill) listSkill.elementAt(i)).update();
        }
//        Debug.d(getGameAction());


    }

    public void switchScreen() {
    }

    public void present(Graphics g) {
        lm.paint(g, 0, 0);
    }

    public void pause() {
    }

    public void resume() {
    }

    public void dispose() {
    }

    public void handleSms(boolean success) {
    }
//======================Key pressed======================

    public void upKeyPressed() {
        switch (character.state) {
            case Character.IDLE:
            case Character.BACKWARD:
            case Character.FORWARD:
            case Character.SIT:
                character.count = -1;
                character.state = Character.JUM;
                break;
        }

    }

    public void downKeyPressed() {
//        character.state = Character.SIT;
        switch (character.state) {
            case Character.IDLE:
                character.count = -1;
                character.state = Character.SIT;
                this.setPreviousKey(Character.SIT);
                break;
        }

    }

    public void rightKeyPressed() {
        switch (character.state) {
            case Character.IDLE:
                character.count = -1;
                if (previousKey == Character.FORWARD && character.left) {
                    character.state = Character.RUN;
                } else {
                    if (character.left) {
                        character.state = Character.FORWARD;
                    } else {
                        character.state = Character.BACKWARD;
                    }
                }
                break;
            case Character.SIT:
                character.count = -1;
                character.state = Character.FORWARD;
                break;
        }
        this.setPreviousKey(Character.FORWARD);
    }

    public void leftKeyPressed() {
        switch (character.state) {
            case Character.IDLE:
                character.count = -1;
                if (previousKey == Character.BACKWARD && !character.left) {
                    character.state = Character.RUN;
                } else {
                    if (character.left) {
                        character.state = Character.BACKWARD;
                    } else {
                        character.state = Character.FORWARD;
                    }
                }
                break;
            case Character.SIT:
                character.count = -1;
                character.state = Character.BACKWARD;
                break;
        }
        this.setPreviousKey(Character.BACKWARD);
    }

    public void fireKeyPressed() {
        switch (character.state) {
            // Idle
            case Character.IDLE:
                switch (previousKey) {
                    case Character.BACKWARD:
                        if (Math.abs(character.getX() - characterAuto.getX()) <= 54) {
                            character.setState(Character.HIGH_KICK);
                        } else {
                            character.setState(Character.KICK_DOWN);
                        }
                        break;
                    case Character.FORWARD:
                    case Character.RUN:
                        if (Math.abs(character.getX() - characterAuto.getX()) <= 55) {
                            character.setState(Character.NEARFIGHT);
                        } else {
                            character.setState(Character.KICK);
                        }
                        break;
                    case Character.SIT:
                        character.setState(Character.PUNCH_DOWN);
                        break;
                    default:
                        character.setState(Character.PUNCH);
//                        character.setState(Character.NEARFIGHT);
                }

                break;
            // Punch
            case Character.PUNCH:
                character.state = Character.REPEATED_PUNCH;
                character.count = -1;
                break;
            case Character.REPEATED_PUNCH:
                character.repeat = true;
                break;
            case Character.FORWARD:
                if (Math.abs(character.getX() - characterAuto.getX()) <= 55) {
                    character.setState(Character.NEARFIGHT);
                } else {
                    character.setState(Character.KICK);
                }

                break;
            case Character.BACKWARD:
                if (Math.abs(character.getX() - characterAuto.getX()) <= 56) {
                    character.setState(Character.HIGH_KICK);
                } else {
                    character.setState(Character.KICK_DOWN);
                }
                break;
            case Character.SIT:
                character.count = -1;
                character.state = Character.PUNCH_DOWN;
                break;
            case Character.JUM:
                character.count = -1;
                character.state = Character.JUMP_FIGHT;
                break;
            case Character.JUMP_FORWARD:
                character.count = -1;
                character.state = Character.JUMP_FORWARD_FIGHT;
                break;
            case Character.JUMP_BACK:
                character.count = -1;
                character.state = Character.JUMP_BACKWARD_FIGHT;
                break;
//            case Character.KICK:
//            case Character.KICK_DOWN:
//            
//
//
//            case Character.PUNCH_DOWN:
//                break;

        }
        setPreviousKey(Character.KICK);
    }

    public void change() {
        character.left = !character.left;
        characterAuto.left = !characterAuto.left;
        this.controlIsLeft = !controlIsLeft;
        character.defineReferencePixel(character.getWidth() / 2, 0);
        characterAuto.defineReferencePixel(characterAuto.getWidth() / 2, 0);
        variableBackUp = character.getFrame();
        if (characterAuto.left) {
            characterAuto.setTransform(Sprite.TRANS_NONE);
            character.setFrame(variableBackUp);
        } else {
            characterAuto.setTransform(Sprite.TRANS_MIRROR);
            character.setFrame(variableBackUp);
        }
        Debug.d("Change========================================");
    }

    public void softKeyLeftPressed() {
    }

    public void softKeyRightPressed() {
    }

    public void numberKeyPressed(int keyCode) {
        switch (keyCode) {
            // jum-back
            case 49:
                controlJum_Backward();
                break;
            // jum 
            case 50:
                upKeyPressed();
                break;
            // jum-for
            case 51:
                controlJum_Forward();
                break;
            // backw
            case 52:
                leftKeyPressed();
                break;
            // forw
            case 54:
                rightKeyPressed();
                break;
            // sit
            case 56:
                downKeyPressed();
                break;
            // fire
            case 53:
                fireKeyPressed();
                break;
            //combo
            case 55:
                comboKeyHandler();
                break;
            case 57:
                character.setState(Character.FATALITY);
                break;
        }

    }
//=======================Key Realeased=================

    public void upKeyRealeased() {
//        switch (character.state) {
//            case Character.IDLE:
//                character.count = -1;
//                character.state = Character.BACKWARD;
//                break;
//        }
    }

    public void downKeyRealeased() {
//        switch (character.state) {
//            case Character.SIT:
//                character.count = -1;
//                character.state = Character.IDLE;
//                break;
//        }
    }

    public void rightKeyRealeased() {
        if (character.left) {
            switch (character.state) {
                case Character.FORWARD:
                case Character.RUN:
                    character.count = -1;
                    character.state = Character.IDLE;
                    break;
            }
        } else {
            switch (character.state) {
                case Character.BACKWARD:
                    character.count = -1;
                    character.state = Character.IDLE;
                    break;
            }
        }


    }

    public void leftKeyRealeased() {
        if (character.left) {
            switch (character.state) {
                case Character.BACKWARD:
                    character.count = -1;
                    character.state = Character.IDLE;
                    break;
                case Character.RUN:
                    character.count = -1;
                    character.state = Character.IDLE;
                    break;
            }
        } else {
            switch (character.state) {
                case Character.FORWARD:
                case Character.RUN:
                    character.count = -1;
                    character.state = Character.IDLE;
                    break;
            }
        }

    }
//=============================== Repeated Key

    public void upKeytRepeated() {
        switch (character.state) {
            case Character.IDLE:
                character.count = -1;
                character.state = Character.JUM;
                break;
        }
    }

    public void downKeyRepeated() {
    }

    public void rightKeyRepeated() {
    }

    public void leftKeyRepeated() {
    }

    public void pointerPressed(int x, int y) {
    }

    public void pointerReleased(int x, int y) {
    }

    public void pointerDragged(int x, int y) {
    }

    protected void paint(Graphics g) {
    }

    private void updateKey() {
        if (previousKey != 0) {
            count++;
            if (count > 7) {
                previousKey = 0;
                count = 0;
            }
        }

    }

    private void setPreviousKey(int key) {
        previousKey = key;
        this.count = 0;
    }

    private void controlJum_Forward() {
        switch (character.state) {
            case Character.IDLE:
            case Character.SIT:
            case Character.RUN:
            case Character.BACKWARD:
            case Character.FORWARD:
                character.count = -1;
                character.state = Character.JUMP_FORWARD;
                break;
        }
    }

    private void controlJum_Backward() {
        switch (character.state) {
            case Character.IDLE:
            case Character.SIT:
            case Character.RUN:
            case Character.BACKWARD:
            case Character.FORWARD:
                character.count = -1;
                character.state = Character.JUMP_BACK;
                break;
        }
    }

    private void comboKeyHandler() {
        switch (character.state) {
            case Character.JUM:
                character.count = -1;
                character.state = Character.SKILL1;
                break;
            case Character.JUMP_BACK:
            case Character.JUMP_FORWARD:
                if (character.getY() + 30 < character.backY) {
                    character.count = -1;
                    character.state = Character.SKILL1;
                    character.setPosition(character.getX(), character.getY() + 30);
                }
                break;
            case Character.IDLE:
            case Character.SIT:
            case Character.RUN:
                if ((Math.abs(character.getX() - characterAuto.getX()) < 60)
                        && (characterAuto.state == Character.IDLE || characterAuto.state == Character.BACKWARD
                        || characterAuto.state == Character.FORWARD)) {
                    character.setState(Character.WRESTLE);
                    characterAuto.setState(Character.BEWRESTLE);
                } else if (previousKey == Character.BACKWARD) {
                    character.setState(Character.SKILL2);
                    previousKey = 0;
                } else {
                    character.setState(Character.SKILL1);
                }
                break;
            case Character.BACKWARD:
                character.count = -1;
                character.state = Character.SKILL2;
                break;
        }
    }
}
